package com.servers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class DatabaseClient {
	private static final String FINISH = "Finish!";
	private static final String U_P_REGEX = ".+?/.+?";
	private static final String LOGINSUCESSFUL = "Login!";
	// private static final String COMMAND_SET_REGEX =
	// "[ALDU]-((PA|PD|DA)|[PD]).*?";
	private static final String COMMAND_SET_REGEX = "((([ADU]-[PD]).+?)|(L-(PA|PD|DA))).*?";
	private static Socket sk;
	private static BufferedReader readerFromServer;
	private static BufferedReader readerFromConsole;
	private static OutputStream out;
	private static String serverLine = null;
	private static String consoleLine = null;

	public static void closeClient() throws IOException {
		System.out.println("Client is quit.");
		readerFromServer.close();
		readerFromConsole.close();
		sk.close();
		return;
	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		sk = new Socket("10.222.232.159", 8888);

		readerFromServer = new BufferedReader(new InputStreamReader(sk.getInputStream()));
		readerFromConsole = new BufferedReader(new InputStreamReader(System.in));
		out = sk.getOutputStream();

		System.out.println("You have connected to port 8888.");
		myLogin();

		while (true) {
			serverLine = readerFromServer.readLine();
			System.out.println(serverLine);
			consoleLine = readerFromConsole.readLine();
			if (consoleLine.equalsIgnoreCase("q")) {
				out.write("Quit\n".getBytes());
				DatabaseClient.closeClient();
				break;
			} else if (consoleLine.equalsIgnoreCase("help")) {
				helpList();
				out.write("Wrongcommand\n".getBytes());
				serverLine = readerFromServer.readLine();
				serverLine = readerFromServer.readLine();
			} else if (consoleLine.matches(COMMAND_SET_REGEX)) {
				out.write((consoleLine + "\n").getBytes());
				System.out.println("******************Command Result**************************");
				while (!(serverLine = readerFromServer.readLine()).equals(FINISH)) {
					System.out.println(serverLine);
				}
				serverLine = "2";
				System.out.println("**********************OVER********************************");
			} else {
//				System.out.println("Wrong command!! Please input again!!");
				out.write("Wrongcommand\n".getBytes());
				serverLine = readerFromServer.readLine();
				System.out.println(serverLine);
				serverLine = readerFromServer.readLine();
			}
		}
	}

	public static void helpList() {
		System.out.println("Command List:");
		System.out.println(
				"Add person:\n\t***A-P pname:BART,birthday:1999-12-31,tel:150****5191,did:12138,salary:99999.99");
		System.out.println("Add department:\n\t***A-D dname:ita,city:zhuhai");
		System.out.println(
				"Update person info:\n\t***U-P id:38324,pname:BART,birthday:1999-12-31,tel:150****5191,did:12138,salary:99999.99");
		System.out.println("Update department info:\n\t***U-D id:14122,dname:hra,city:hongkong");
		System.out.println("Delete person:\n\t***D-P id:4546");
		System.out.println("Delete department:\n\t***D-D did:2333");
		System.out.println("Query all person info:\n\t***L-PA");
		System.out.println("Query all departments info:\n\t***L-DA");
		System.out.println("Query all person info within one department:\n\tL-PD");
		System.out.println("Input 'Q' to quit.");
	}

	public static void myLogin() throws Exception {
		while (true) {
			serverLine = readerFromServer.readLine();
			System.out.println(serverLine);
			consoleLine = readerFromConsole.readLine();
			if (consoleLine.equalsIgnoreCase("q")) {
				DatabaseClient.closeClient();
				return;
			} else if (consoleLine.matches(U_P_REGEX)) {
				out.write((consoleLine + "\n").getBytes());
				if ((serverLine = readerFromServer.readLine()).equals(LOGINSUCESSFUL)) {
					System.out.println(serverLine);
					System.out.println("Welcome " + consoleLine.substring(0, consoleLine.indexOf("/")) + " !!");
					break;
				} else {
					System.out.println(serverLine);
				}
			} else {
				System.out.println("Please insert '/' between username and password.");
				out.write("123/234343\n".getBytes());
				serverLine = readerFromServer.readLine();
			}
		}
	}
}
